package com.sky.service.impl;



import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.handler.GlobalExceptionHandler;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 员工管理系统 项目
 */
@Slf4j
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult findCategory(CategoryPageQueryDTO categoryPageQueryDTO) {

        //开始分页
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        Page<Category> page = categoryMapper.findCategoryByName(categoryPageQueryDTO);
        long total = page.getTotal();
        List<Category> result = page.getResult();
        return new PageResult(total, result);
    }


    @Override
    public void add(CategoryDTO categoryDTO) {
        Category category = new Category();
        //参数拷贝
        BeanUtils.copyProperties(categoryDTO, category);
        //设置创建时间和修改时间
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        //设置状态为禁用
        category.setStatus(StatusConstant.DISABLE);
        //设置创建人、修改人
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());

        log.info("添加分类信息：{}", category);

        categoryMapper.insert(category);
    }

    /**
     * 删除分类
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        //查询是否存在该分类的套餐
        List<Setmeal> setmeals = setmealMapper.selectSetmealBycategoryId(id);
        //查询是否存在该分类的菜品
        List<Dish> dishes = dishMapper.SelectDishByCategoryId(id);
        log.info("套餐有:{},菜品有:{}", setmeals,dishes);
        if(setmeals.size() != 0){
            throw new RuntimeException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }else if(dishes.size() != 0){
            throw new RuntimeException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }else{
            categoryMapper.deleteById(id);
        }
    }

    /**
     * 修改分类
     * @param categoryDTO
     */
    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        log.info("修改分类信息：{}", category);

        categoryMapper.update(category);
    }

    /**
     * 修改分类状态
     * @param status
     * @param id
     */
    @Override
    public void updateStatus(Integer status, Long id) {
        categoryMapper.updateStatusById(status, id);
        log.info("修改分类状态：{},{}", status,id);
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @Override
    public List<Category> findCategoryByType(Integer type) {

        List<Category> list = categoryMapper.findCategoryByType(type);

        return list;
    }

    /**
     * 根据类型查询所有分类
     * @return
     */
    @Override
    public List<Category> list(Integer type) {
        List<Category> list = categoryMapper.findCategoryByType(type);
        log.info("===========>查询分类：{}", list);
        return list;
    }
}
